<?php

namespace App\Console\Commands;

use App\Library\Logic\BankLogic;
use App\Library\Logic\ShopBankCardLogic;
use App\Library\Logic\UserAccountLogic;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class SyncBankCardCommand extends Command
{
    private static string $APPID = 'COViGAey';

    private static string $APPKey = 'pzZq4QFS';

    /**
     * 验证银行是不否真实
     *
     * @var string
     */
    protected $signature = 'command:bank';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $model = ShopBankCardLogic::query()->get();

        if (!is_null($model)) {
            foreach ($model as $item) {
                if ((int)$item->bc_type === 3) {
                    if (is_null(BankLogic::findByWhere(['card_no' => $item->bc_number]))) {
                        $client = new Client;
                        $response = $client->post(__('base.bankApi') . $item->bc_number);
                        $res = json_decode($response->getBody()->getContents());
                        if ($res->validated === false) return fail(638);
                        else if ($res->cardType === 'CC') return fail(639);

                        $bankInfo = self::bank($item->bc_name, $item->bc_number);

                        if (empty($bankInfo['code'])) {
                            return failMessage($bankInfo['message']);
                        } else if ($bankInfo['chargeStatus'] === 0) {
                            return failMessage($bankInfo['message']);
                        } else if ($bankInfo['data']['result'] === '02') {
                            return failMessage($bankInfo['message']);
                        } else if ($bankInfo['data']['cardCategory'] !== '借记卡') {
                            $msg = '该银行卡不是借记卡类型';
                            return failMessage($msg);
                        }

                        BankLogic::insertForModel([
                            'userId' => $item->user_id,
                            'instId' => $res->bank,
                            'bankName' => $bankInfo['data']['bankName'],
                            'cardNo' => $item->bc_number,
                            'info' => $bankInfo
                        ]);

                        UserAccountLogic::insertForModel([
                            'userId' => $item->user_id,
                            'paymentMethodId' => 3,
                            'name' => $item->bc_name,
                            'identity' => $item->bc_number,
                            'nickname' => $bankInfo['data']['bankName'],
                        ]);
                    }
                } else {
                    $type = (int)$item->bc_type === 1 ? 2 : 1;
                    if (is_null(UserAccountLogic::findByWhere(['paymentMethodId' => $type, 'identity' => $item->bc_number]))) {
                        UserAccountLogic::insertForModel([
                            'userId' => $item->user_id,
                            'paymentMethodId' => $type,
                            'name' => $item->bc_name,
                            'identity' => $item->bc_number,
                            'nickname' => $item->bc_name
                        ]);
                    }
                }
            }
        }
    }

    private function bank($name, $cardNo)
    {
        $client = new Client;
        $response = $client->post('https://api.253.com/open/bankcard/card-two-auth', [
            'verify' => false,
            'form_params' => [
                'appId' => self::$APPID,
                'appKey' => self::$APPKey,
                'name' => $name,//姓名
                'cardNo' => $cardNo,//银行卡号，限单个
            ],
        ]);
        return json_decode($response->getBody()->getContents(), true);
    }
}
